package com.lys.emsystem.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.emsystem.entity.Teach;
import com.lys.emsystem.entity.Teacher;
import com.lys.emsystem.mapper.TeachMapper;
import com.lys.emsystem.service.IIntentService;
import com.lys.emsystem.service.ITeachService;
import com.lys.emsystem.service.ITeacherService;
import com.lys.emsystem.utils.JWTUtils;
import com.lys.emsystem.utils.Result;
import com.lys.emsystem.vo.StuJobVo;
import com.lys.emsystem.vo.TeacherVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liyansheng
 * @since 2022-04-27
 */
@RestController
@CrossOrigin
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private ITeacherService teacherService;

    @Autowired
    private ITeachService teachService;

    @PostMapping
    public Result search(@RequestBody TeacherVo teacherVo) {
        IPage<Teacher> page;
        page = teacherService.search(new Page<Teacher>(teacherVo.getPageIndex(), teacherVo.getPageSize()),
                teacherVo);
        if (page.getSize() > 0) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("totalCount", page.getTotal());
            map.put("list", page.getRecords());
            return Result.buildResult(Result.Status.OK, map);
        } else {
            return Result.buildResult(Result.Status.OK, null);
        }

    }

    @PostMapping("/add")
    public Result add(@RequestBody TeacherVo teacherVo) {
        System.out.println(teacherVo);
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherVo, teacher);
        System.out.println(teacher);
        // 设置默认密码
        if (teacher.getPassword() == null) {
            teacher.setPassword(DigestUtils.md5DigestAsHex(teacher.getAccount().substring(4).getBytes()));
        }
        //设置默认性别
        if (teacher.getSex() == null) {
            teacher.setSex(true);
        }
        if (teacherService.save(teacher)) {
            //保存教师教班级信息
            int count = 0;
            Integer classes[] = teacherVo.getClassroomList();
            for (int i = 0; i < classes.length; i++) {
                Teach teach = new Teach();
                teach.setTeacherId(teacher.getId());
                teach.setClassroomId(classes[i]);
                teachService.save(teach);
                count = count + 1;
            }
            if (count == classes.length) {
                return Result.buildResult(Result.Status.OK);
            }
            return Result.buildResult(Result.Status.ERROR);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Teacher teacher) {
        System.out.println(teacher);
        if (teacherService.updateById(teacher)) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/update2")
    public Result update2(@RequestBody Teacher teacher) {
        System.out.println(teacher);
        if (teacherService.updateById(teacher)) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        if (teacherService.removeById(id)) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/delList")
    public Result deleteList(@RequestBody TeacherVo teacherVo) {
        if (teacherService.removeByIds(Arrays.asList(teacherVo.getIds()))) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @GetMapping
    public Result getTeacherInfoById(@RequestHeader("token") String token) {
        if (StringUtils.isEmpty(token)) {
            return Result.buildResult(Result.Status.ERROR,"请先登录！");
        }
        Integer userId = JWTUtils.getUserId(token);
        Teacher teacher = teacherService.selectTeacherInfoById(userId);
        return Result.buildResult(Result.Status.OK, teacher);
    }

    @GetMapping("/stuJobs")
    public Result getStudentJobStatus(@RequestHeader("token") String token) {
        if (StringUtils.isEmpty(token)) {
            return Result.buildResult(Result.Status.ERROR,"请先登录！");
        }
        List<StuJobVo> stuJobStatus = teacherService.getStuJobStatus(JWTUtils.getUserId(token));
        return Result.buildResult(Result.Status.OK, stuJobStatus);
    }

    @GetMapping("/analyze")
    public Result getStudentData(@RequestHeader("token") String token) {
        if (StringUtils.isEmpty(token)) {
            return Result.buildResult(Result.Status.ERROR,"请先登录！");
        }
        Integer userId = JWTUtils.getUserId(token);
        return teacherService.getAnalyzeData(userId);
    }

    @GetMapping("/analyze/intent")
    public Result getStudentIntent(@RequestHeader("token") String token) {
        Integer userId = JWTUtils.getUserId(token);
        return teacherService.getAnalyzeIntent(userId);
    }
}
